function ListNode(val, next) {
  this.val = val === undefined ? 0 : val;
  this.next = next === undefined ? null : next;
}
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var sortList = function (head) {
  if (!head || !head.next) return head;
  let l = head.val;
  let r = head.val;
  let mid = null;
  let p = head;
  let q = null;
  let h1 = null;
  let h2 = null;
  //   获得最大值或者最小值;
  while (p) {
    l = Math.min(p.val, l);
    r = Math.max(p.val, r);
    p = p.next;
  }
  if (l == r) return head;
  //   中间值
  mid = Math.floor((l + r) / 2);
  //   开始分区
  p = head;
  while (p) {
    q = p.next;
    if (p.val <= z) {
      // 分配到第一个链表
      p.next = h1;
      h1 = p;
    } else {
      p.next = h2;
      h2 = p;
    }
    p = q;
  }
  h1 = sortList(h1);
  h2 = sortList(h2);
  //   合并
  p = h1;
  while (p) {
    p = p.next;
  }
  p.next = h2;
  return h1;
};
